/**
 *
 * author: wangml
 * desc: 公共导出方法
 */

// 导出 excel 文件
export const exportExcel = function(url, filename, formField) {
  // fileName -> 选装包维护20201222181629.xlsx
  const title = filename || this.$route.matched[this.$route.matched.length - 1].meta.title.replace('/', '')
  const fileSuffix = this.$config.excelExp
  const fileName = this.$utils.fileNameWithTime(`${title}${fileSuffix}`)
  const loading = this.$loading({ text: `正在导出： ${fileName},请稍等...` })
  this.$requestAPI({
    apiUrl: url,
    variables: { ...(formField || this.formField), dataType: 'excel' }
  }).then(res => {
    this.$utils.downloadFile(res, fileName)
  }).catch(err => {
    if (err.response.data.msg) {
      this.$message(err.response.data.msg)
    } else {
      this.$message(err.message)
    }
  }).finally(() => {
    loading.close()
  })
}
